package com.fufu.leetcode;

import java.util.Arrays;

/**
 * @author: BlcakTiger
 * @date: 2023/2/26 9:26
 * @contact: hsf6661@163.com
 */
public class 寻找两个正序数组的中位数 {

    public static void main(String[] args) {
        int[] arr1 = {};
        int[] arr2 = {2,3};
        double medianSortedArrays = findMedianSortedArrays(arr1, arr2);
        System.out.println(medianSortedArrays);
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int merge[] = new int[nums1.length+nums2.length];
        int firIndex = 0;
        int secIndex = 0;
        int curIndex = 0;
        while (curIndex<merge.length){
            //Neither nums1 nor nums2 is empty
            if (firIndex < nums1.length && secIndex < nums2.length){
                if (nums1[firIndex] < nums2[secIndex]){
                    merge[curIndex] = nums1[firIndex];
                    curIndex += 1;
                    firIndex += 1;
                }else {
                    merge[curIndex] = nums2[secIndex];
                    curIndex += 1;
                    secIndex += 1;
                }
            }else{//either nums1 or nums2 is empty
                if (firIndex < nums1.length){
                    merge[curIndex] = nums1[firIndex];
                    curIndex += 1;
                    firIndex += 1;
                }
                if (secIndex < nums2.length){
                    merge[curIndex] = nums2[secIndex];
                    curIndex += 1;
                    secIndex += 1;
                }
            }
        }
        System.out.println(Arrays.toString(merge));
        //odd or even ?
        if ((merge.length&1) == 1){
            return merge[merge.length/2];
        }else {
            return (merge[merge.length/2-1]+merge[merge.length/2])/2.0;
        }
    }
}
